﻿using SFML.Graphics;

namespace Discipulus
{
	class Frame : Control
	{
		private Shape background;
		private Sprite[] sprites = new Sprite[8];

		public Frame(IntRect bounds = new IntRect(), Control parent = null, string skin = "DefaultSkin.png")
			: base(bounds, parent, skin)
		{
			for (int i = 0; i < sprites.Length; i++)
				sprites[i] = new Sprite(Skin);

			// Top-left.

			sprites[0].SubRect = new IntRect(0, 0, 7, 24);

			// Top.

			sprites[1].Position = new Vector2(7, 0);
			sprites[1].SubRect = new IntRect(7, 0, 50, 24);
			sprites[1].Width = Bounds.Width - 14;

			// Top-right.

			sprites[2].Position = new Vector2(7 + sprites[1].Width, 0);
			sprites[2].SubRect = new IntRect(57, 0, 7, 24);

			// Left.

			sprites[3].Position = new Vector2(0, 24);
			sprites[3].SubRect = new IntRect(0, 24, 7, 33);
			sprites[3].Height = Bounds.Height - 31;

			// Right.

			sprites[4].Position = new Vector2(7 + sprites[1].Width, 24);
			sprites[4].SubRect = new IntRect(57, 24, 7, 33);
			sprites[4].Height = Bounds.Height - 31;

			// Bottom-left.

			sprites[5].Position = new Vector2(0, Bounds.Height - 7);
			sprites[5].SubRect = new IntRect(0, 57, 7, 7);

			// Bottom.

			sprites[6].Position = new Vector2(7, Bounds.Height - 7);
			sprites[6].SubRect = new IntRect(7, 57, 50, 7);
			sprites[6].Width = Bounds.Width - 14;

			// Bottom-right.

			sprites[7].Position = new Vector2(Bounds.Width - 7, Bounds.Height - 7);
			sprites[7].SubRect = new IntRect(57, 57, 7, 7);

			// Background.

			background = Shape.Rectangle(new FloatRect(7, 24, Bounds.Width - 14, Bounds.Height - 31), Color.White);
		}

		public override void OnSkinChanged()
		{
			foreach (var sprite in sprites)
				sprite.Image = Skin;
		}

		public override void DoDraw()
		{
			texture.Draw(background);

			foreach (var sprite in sprites)
				texture.Draw(sprite);
		}
	}
}